<?php
/**
 * This file is part of Hyperf Mall.
 *
 * @link     https://gitee.com/scwlkj/hyperf_mall
 * @contact  hyperf_mall@163.com
 */

namespace App\Repository;

use App\Constants\OrderConstant;
use App\Exception\BaseException;
use App\Model\CommentModel;
use App\Service\Goods\GoodsService;
use App\Service\Member\MemberService;
use App\Utils\DB;
use App\Utils\Log;
use Hyperf\Di\Annotation\Inject;
use Swoole\Exception;

class CommentRepository extends BaseRepository
{
    /**
     * @Inject
     * @var CommentModel
     */
    protected $model;

    /**
     * @Inject
     * @var MemberService
     */
    protected MemberService $memberService;

    /**
     * @Inject
     * @var OrderRepository
     */
    protected OrderRepository $orderRepository;

    /**
     * @Inject
     * @var OrderDetailRepository
     */
    protected OrderDetailRepository $orderDetailRepository;

    /**
     * @Inject
     * @var GoodsCommentRepository
     */
    protected GoodsCommentRepository $goodsCommentRepository;

    /**
     * @Inject
     * @var GoodsService
     */
    protected GoodsService $goodsService;

    /**
     * Notes: C端添加评论
     * Author: Bruce.z
     * DateTime: 2022/8/17 20:17
     * @param $data
     * @return bool
     * @throws BaseException
     */
    public function baseAdd($data): bool
    {
        $member = $this->memberService->getOne(['id'=>$data['member_id']], ['username','image']);
        $order = $this->orderRepository->getOne(['oid'=>$data['oid']],['create_time','oid','status']);
        if($order['status'] != OrderConstant::ORDER_STATUS_ESTIMATE){
            throw new BaseException(BaseException::BASE_ERROR, '该订单状态下不允许评论!');
        }
        $order_detail = $this->orderDetailRepository->getRows(['oid'=>$data['oid']], ['id','num','goods_id','product_id']);
        $order_detail = array_column($order_detail, null, 'id');
        $re = [
            'member_id'=>$data['member_id'],
            'member_image'=>$member['image'],
            'nickname'=>$member['username'],
            'oid'=>$data['oid'],
            'delivery_speed_mark'=>$data['logistics'],
            'delivery_service_mark'=>$data['service'],
            'purchase_time'=>$order['create_time'],
        ];

        $detail = [];
        foreach ($data['formData'] as $g){
            $id = $g['id'];
            $detail[] = [
                'member_id'=>$data['member_id'],
                'goods_id'=>$order_detail[$id]['goods_id']??0,
                'product_id'=>$order_detail[$id]['product_id']??0,
                'member_image'=>$member['image'],
                'nickname'=>$member['username'],
                'oid'=>$data['oid'],
                'saas_id'=>get_saas_id(),
                'purchase_time'=>$order['create_time'],
                'content'=>$g['cont'],
                'num'=>$order_detail[$id]['num'],
                'goods_mark'=>$g['btnnum'],
                'update_time'=>time(),
                'create_time'=>time(),
                'image'=>$g['images'] ? serialize($g['images']) : '',
            ];
        }

        DB::beginTransaction();
        try {
            parent::baseAdd($re);
            $this->goodsCommentRepository->batchAdd($detail);

            $this->orderRepository->updateByConditions(['oid'=>$data['oid']],
                [
                    'status'=>OrderConstant::ORDER_STATUS_FINISH,
                    'is_comment'=>OrderConstant::ORDER_TYPE_SELL
                ]
            );
            DB::commit();
        } catch (Exception $e){
            DB::rollBack();
            Log::getInstance()->error('添加评论', [$e->getMessage()]);
            return false;
        }
        return true;
    }

    /**
     * Notes: 通过商品评论id获取评价详情
     * Author: Bruce.z
     * DateTime: 2022/10/22 14:45
     * @param int $id
     * @return array
     */
    public function commentDetail(int $id): array
    {
        $re = $this->goodsCommentRepository->getOne(['id'=>$id], ['oid','id','goods_mark','content','image','nickname','member_image','goods_id']);

        $comment = $this->getOne(['oid'=>$re['oid']],['delivery_speed_mark','delivery_service_mark','is_anonymous','create_time']);

        $data = array_merge($re, $comment);
        $goods = $this->goodsService->getOne(['id'=>$re['goods_id']],['name','img']);
        $data = array_merge($data, $goods);
        $data['image'] = unserialize($data['image']);

        return  $data;
    }
}
